from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAdminUser
from meiduo_admin.serializers.increment import GoodsVisitCountSerializer

from users.models import User
from datetime import date,timedelta
from goods.models import GoodsVisitCount


class IncrementTotalCountView(APIView):
    '''
    注册用户总量统计
    '''
    permission_classes = [IsAdminUser]

    def get(self,request):
        # 1.查询注册总量
        count=User.objects.all().count()
        # 2.获取当前日期
        now_date =date.today()
        # 3.返回结果
        return Response({
            'count':count,
            'date':now_date
        })


class IncrementDayCountView(APIView):
    '''
    获取当天注册用户总量统计
    '''
    permission_classes = [IsAdminUser]

    def get(self,request):
        # 1.获取当前日期
        now_date = date.today()
        # 2.查询注册总量
        count=User.objects.filter(date_joined__gte=now_date).count()
        # 3.返回结果
        return Response({
            'count':count,
            'date':now_date
        })


class IncrementDayActiveView(APIView):
    '''
    获取当天活跃用户总量统计
    '''
    permission_classes = [IsAdminUser]

    def get(self,request):
        # 1.获取当前日期
        now_date = date.today()
        # 2.查询注册总量
        count=User.objects.filter(last_login__gte=now_date).count()
        # 3.返回结果
        return Response({
            'count':count,
            'date':now_date
        })


class IncrementDayOrdersView(APIView):
    '''
    获取当天下单用户总量统计
    '''
    permission_classes = [IsAdminUser]

    def get(self,request):
        # 1.获取当前日期
        now_date = date.today()
        # 2.查询注册总量
        count=User.objects.filter(orders__create_time__gte=now_date).count()
        # 3.返回结果
        return Response({
            'count':count,
            'date':now_date
        })


class IncrementMonthCountView(APIView):
    '''
    获取当月每天下单用户总量统计
    '''
    permission_classes = [IsAdminUser]

    def get(self,request):
        # 1.获取当前日期
        now_date = date.today()
        start_date=now_date-timedelta(days=29)
        date_list=[]
        for i in range(30):
            # 获取起始时间
            index_date=start_date+timedelta(days=i)
            # 第二天时间
            next_date=index_date+timedelta(days=1)

            count=User.objects.filter(date_joined__gte=index_date,date_joined__lt=next_date).count()
            date_list.append({
                'count':count,
                'date':index_date
            })
        # 3.返回结果
        return Response(date_list)


class GoodsDayView(APIView):
    '''
    获取当前商品日访问量统计
    '''
    permission_classes = [IsAdminUser]

    def get(self,request):
        # 1.获取当前日期
        now_date = date.today()
        # 2.查询注册总量
        goodsvisit=GoodsVisitCount.objects.filter(date__gte=now_date)
        # 3.返回结果
        ser=GoodsVisitCountSerializer(goodsvisit,many=True)
        print(ser)
        print(ser.data)
        return Response(ser.data)




